def trim(s):
    if s == None or s == "":
        return ""
    first = -1
    last = -1
    for i, str in enumerate(s):
        if first == -1 and str != " ":
            first = i
            break
    for i, str in enumerate(reversed(s)):
        if last == -1 and str != " ":
            last = len(s) - i
            break
    s = s[first:last]
    return s

# 测试:
if trim('hello  ') != 'hello':
    print(trim('hello  '))
    print('测试失败!')
elif trim('  hello') != 'hello':
    print('测试失败!')
elif trim('  hello  ') != 'hello':
    print('测试失败!')
elif trim('  hello  world  ') != 'hello  world':
    print('测试失败!')
elif trim('') != '':
    print('测试失败!')
elif trim('    ') != '':
    print('测试失败!')
else:
    print('测试成功!')
